//
//  UserAccount.swift
//  SinaWeiboDemo
//
//  Created by 伍禄林 on 16/9/23.
//  Copyright © 2016年 伍禄林. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding{
    
    var access_token: String?
    
    var remind_in: String?
    
    var uid: String?
    
    var avatar_large:String?
    
    var name: String?
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeys(dict)
    }
    
    required init?(coder aDecoder: NSCoder) {//解归档
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        remind_in = aDecoder.decodeObject(forKey: "remind_in") as? String
        uid = aDecoder.decodeObject(forKey: "uid") as? String
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
        name = aDecoder.decodeObject(forKey: "name") as? String
        
    }
    
    
    func encode(with aCoder: NSCoder) {//归档
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(remind_in, forKey: "remind_in")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(avatar_large, forKey: "avatar_large")
        aCoder.encode(name, forKey: "name")


        
    }
    override var description: String {
        let keys = ["access_token","remind_in","uid","avatar_large","name"];
        return dictionaryWithValues(forKeys: keys).description
    }
    
    
    func saveAccount() -> Void {
        
        let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String).appending("/accout.plist")
        print("path=======\(path)")
        NSKeyedArchiver.archiveRootObject(self, toFile: path);
        
        
    }
    
    
    //方法前加class 表示类方法
    class func loadAccount() ->  UserAccount? {
        
        let path = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String).appending("/account.plist")
        
        print("patht============\(path)")
        
        if let account = NSKeyedUnarchiver.unarchiveObject(withFile: path) as? UserAccount {
            
            return account
        }
        
       return nil
        
    }
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
    }
    
    
}
